import { createSlice } from "@reduxjs/toolkit";
import { medicineInterface } from "@/Type/git.ts";

interface CounterState {
  num?: number;
  cartlist?: number[];
  allcheckedstate?: boolean;
  allprice?: number;
}

const initialState: CounterState = {
  num: 0,
  cartlist: JSON.parse(localStorage.getItem("cartlist") || "[]"),
  allcheckedstate: false,
  allprice: 0,
};

export const counterSlice = createSlice({
  name: "cart",
  initialState,
  reducers: {
    // 添加
    add: (state, action) => {
      const {
        item: { id },
        num,
      } = action.payload;
      const item: medicineInterface = state.cartlist?.find(
        (item) => item.id === id
      );
      if (item) {
        item.num += num;
      } else {
        console.log({ ...action.payload.item, num, checked: true });
        state.cartlist?.push({
          ...action.payload.item,
          num,
          checked: true,
        });
      }
      localStorage.setItem("cartlist", JSON.stringify(state.cartlist));
    },
    // 数量改变
    change: (state, action) => {
      const { id, num } = action.payload;
      const item: medicineInterface = state.cartlist?.find(
        (item) => item.id === id
      );
      if (item) {
        item.num = num;
      }
      state.allprice = state.cartlist?.reduce(
        (pre, next) => pre + (next.checked ? next.num * next.price : 0),
        0
      );
      localStorage.setItem("cartlist", JSON.stringify(state.cartlist));
    },
    // 单选
    checked: (state, action) => {
      const { id, checked } = action.payload;
      const item: medicineInterface = state.cartlist?.find(
        (item) => item.id === id
      );
      if (item) {
        item.checked = checked;
      }
      state.allcheckedstate = state.cartlist?.every((item) => item.checked); // 全选状态
      state.allprice = state.cartlist?.reduce(
        (pre, next) => pre + (next.checked ? next.num * next.price : 0),
        0
      );
      localStorage.setItem("cartlist", JSON.stringify(state.cartlist));
    },
    // 全选
    allchecked: (state, action) => {
      state.allcheckedstate = action.payload;
      state.cartlist?.forEach(
        (item: medicineInterface) => (item.checked = action.payload)
      );
      state.allprice = state.cartlist?.reduce(
        (pre, next) => pre + (next.checked ? next.num * next.price : 0),
        0
      ); // 总价计算
      localStorage.setItem("cartlist", JSON.stringify(state.cartlist));
    },
    // 删除
    remove: (state, action) => {
      const id = action.payload;
      console.log(id);
      state.cartlist = state.cartlist?.filter((item) => item.id !== id);
      localStorage.setItem("cartlist", JSON.stringify(state.cartlist));
    },
    // 清空
    clear: (state) => {
      state.cartlist = [];
      localStorage.setItem("cartlist", JSON.stringify(state.cartlist));
    },
  },
});

export const {
  add,
  change,
  allcheckedstate,
  allprice,
  checked,
  allchecked,
  remove,
  clear,
} = counterSlice.actions;
export default counterSlice.reducer;
